package org.xujin.book.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.xujin.book.po.Student;
/**
 * 
* @ClassName: Update
* @Description: TODO
* @author Xu,Jin Software_King@qq.com
* @Site:http://www.xujin.org
 */
public class Update {
	public static void main(String[] args) {
		/*创建一个Configuration()的实例，调用configure()方法加载hibernate.cfg.xml配置文件
		 * 如果hibernate.cfg.xml没有放在默认的位置:classpath下
		 * 举例：Configuration conf = new Configuration().configure("/hibernate.cfg.xml配置文件的路径/hibernate.cfg.xml");
		 * 
		 * */
		Configuration conf = new Configuration().configure();
		/*conf调用buildSessionFactory()方法创建SessionFactory,相当于创建数据源*/
		SessionFactory sf = conf.buildSessionFactory();
		/*打开会话*/
		Session session = sf.openSession();
		Student stu = (Student) session.get(Student.class, "2010023011");
		if (stu != null) {
			stu.setName("王五");
			// 修改
			Transaction tran = session.beginTransaction();
			session.update(stu);
			tran.commit();
		}		
		// 关闭session
		session.close();
	}
}
